﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AsPlugInManager;
using AsLib;
using System.Reflection; 

namespace NNLInterface
{
    public partial class frmSplictOrder : NNLInterface.frmBaseXC
    {

        ASBOrder orderNot;

        public frmSplictOrder()
        {
            InitializeComponent();
        }

        public frmSplictOrder(IASPArgList argList, ASBOrder orderNo)
        {
            InitializeComponent();
            common.agrs = (ASPArgList)argList;
            orderNot = orderNo;
        }

        private void frmSplictOrder_Load(object sender, EventArgs e)
        {
            this.txtOrderNo.Text = orderNot.Code;
            //this.dtePlanDate.EditValue =orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCLET")), 1);
            this.dtePlanDate.EditValue =Convert.ToDateTime( orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCLET")), 1));
            this.calPlanQty.Value =(decimal) orderNot.Qty;
            this.dteEst.EditValue =(DateTime) orderNot.EST;
            this.dteEstNew.EditValue = (DateTime)orderNot.EST.AddDays(2);

        }

        private void frmSplictOrder_FormClosing(object sender, FormClosingEventArgs e)
        {
            orderNot = null;
        }



        //订单分割
        private void splictOrderQty()
        {
           int orderQty =(int)System.Math.Floor(orderNot.Qty) ;
           double orderQtyBalance = orderNot.Qty - orderQty;
           int orderNum = orderQty /(int) this.calSplictQty.Value;


           if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要将分割订单吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) return;

           for (int i = 0; i < orderNum; i++)
           {
               AsLib.ASBOrder order = common.pro.RootOrder.AddChildAsOrder(orderNot.Code + '-' + (i+1).ToString());
               PropertyInfo[] infosnew = order.GetType().GetProperties();

               //复制原始订单的属性
               CopyOrderProperty(order, infosnew);

               // 需要将其他自定义字段的值加上去
               CopyOrderProperty(orderNot, order);
              
               //设定数量及最早开始时间
               order.Qty = (int)this.calSplictQty.Value;
               order.EST =Convert.ToDateTime( this.dteEst.Text);
               string  dteTmp = Convert.ToDateTime(this.dteEstNew.Text).AddDays(i * (double)this.calDays.Value).ToString("yyyy/MM/dd HH:mm:ss",System.Globalization.DateTimeFormatInfo.InvariantInfo);
               order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UFEST")), 1, dteTmp );

           }

           //如果有余数
          if (orderQty %this.calSplictQty.Value!= 0 || orderQtyBalance!=0)
          {
              AsLib.ASBOrder order = common.pro.RootOrder.AddChildAsOrder(orderNot.Code + '-' + (orderNum + 1).ToString());
              PropertyInfo[] infosnew = order.GetType().GetProperties();
              //复制原始订单的属性
              CopyOrderProperty(order, infosnew);

              // 需要将其他自定义字段的值加上去
              CopyOrderProperty(orderNot, order);

              //设定数量及最早开始时间(向后面的日期加)
              order.Qty = (double)(orderQty % this.calSplictQty.Value) + orderQtyBalance;
              order.EST = Convert.ToDateTime(this.dteEst.Text);
              string dteTmp = Convert.ToDateTime(this.dteEstNew.Text).AddDays(orderNum * (double)this.calDays.Value).ToString("yyyy/MM/dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
              order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UFEST")), 1, dteTmp);

              orderNum++;

          }
          
          //删除以前的旧工单
          DeleteOrder(orderNot);

          common.pro.BroadcastChanges();
          DevExpress.XtraEditors.XtraMessageBox.Show("此制令单(" + orderNot.Code + ") 已成功分割为" + orderNum.ToString() + "笔新制令单!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);

        
        }


        public void CopyOrderProperty(AsLib.ASBOrder orderNot, AsLib.ASBOrder order)
        {
            //客户最终交期
            string strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCDLET")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCDLET")), 1, strTmpProperty);

            //配方号
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UBOM")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UBOM")), 1, strTmpProperty);

            //物料1次复期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST1")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST1")), 1, strTmpProperty);

            //物料2次复期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST2")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST2")), 1, strTmpProperty);

            //物料3次复期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST3")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST3")), 1, strTmpProperty);

            //物料4次复期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST4")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST4")), 1, strTmpProperty);

            //物料5次复期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST5")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UEST5")), 1, strTmpProperty);

            //客户订单
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCODR")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCODR")), 1, strTmpProperty);

            //最终部门
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Spec2")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Spec2")), 1, strTmpProperty);


            //品名规格
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Spec1")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Spec1")), 1, strTmpProperty);


            //订单数量
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_NumSpec1")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_NumSpec1")), 1, strTmpProperty);

            //需求日期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCLET")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCLET")), 1, strTmpProperty);

            //备注
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Comments")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Comments")), 1, strTmpProperty);


            //客户
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Customer")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Customer")), 1, strTmpProperty);

            //原始工单号
            //strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UERPMONO")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UERPMONO")), 1, orderNot.Code);

            //版本号
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Spec4")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_Spec4")), 1, strTmpProperty);

     
            //接单日期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_OrderTime")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "Order_OrderTime")), 1, strTmpProperty);

            //客供料复期
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCustLET")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UCustLET")), 1, strTmpProperty);

            //mo类别
            strTmpProperty = orderNot.GetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UMOType")), 1);
            order.SetAsStr((TPropertyID)Convert.ToInt32(common.GetLanguageWord("PropertyID", "OrderUser_UMOType")), 1, strTmpProperty);


        }

        //遍历所有的订单数据，找出存在原始订单号的工单数据，删除即可
        private static void DeleteOrder( AsLib.ASBOrder orderNot )
        {
           for (int i = 0; i < common.pro.RootOrder.ChildCount; i++)
           {
           	   AsLib.ASBOrder order=common.pro.RootOrder.get_ChildAsOrder(i);
               if (order.ObjectID==orderNot.ObjectID)
               {
               	   common.pro.RootOrder.DeleteChild(i);
               }
           }
        }

        private void CopyOrderProperty(AsLib.ASBOrder order, PropertyInfo[] infosnew)
        {
            PropertyInfo[] infosold = ((ASBOrder)orderNot).GetType().GetProperties();

            foreach (PropertyInfo itemNew in infosnew)
            {
                foreach (PropertyInfo itemold in infosold)
                {
                    if (itemNew.Name == itemold.Name && itemNew.CanWrite && itemNew.Name.ToLower() != "code")
                    {

                        if (itemNew.GetIndexParameters().Length == 0)
                            itemNew.SetValue(order, itemold.GetValue(orderNot, null), null);
                        else
                            itemNew.SetValue(order, itemold.GetValue(orderNot, new object[] { 0 }), new object[] { 0 });
                    }
                }
            }
        }

        private bool CheckData()
        {
            if (this.calSplictQty.Value== 0 || this.calSplictQty.Value>this.calPlanQty.Value)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("分割批量不能为为0或者大于原始计划数量，请检查!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.calSplictQty.Focus();
                return false;
            }
            return true;
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (!CheckData()) return;
            splictOrderQty();
            this.Close();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
